package com.cloud.easyExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DrugInstructionImport {
    public static void main(String[] args) throws Exception{
        final List list = new ArrayList();
        //使用EasyExcel读取test1.xlsx文件
        EasyExcel.read("src/main/resources/drug-instruction.xls", DrugInstruction.class, new AnalysisEventListener<DrugInstruction>() {
                    //重写子类方法
                    @Override
                    public void invoke(DrugInstruction drugInstruction, AnalysisContext analysisContext) {
                        list.add(drugInstruction);
                    }
                    //重写子类方法
                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                    }

                    @Override
                    public void invokeHeadMap(Map headMap, AnalysisContext context) {
                        System.out.println(headMap);
                    }
                }
        ).doReadAll();

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //设置事务自动提交
        SqlSession session = sqlSessionFactory.openSession(true);
        DrugInstructionMapper instructionMapper = session.getMapper(DrugInstructionMapper.class);
        Integer count = instructionMapper.selectCount();
        //获取读取到的数据
        for (Object o : list) {
            DrugInstruction drugInstruction = (DrugInstruction) o;
            System.out.println(drugInstruction);
            Integer i = instructionMapper.insertDrugInstruction(drugInstruction);
            System.out.println(i);
        }
    }
}
